DodoRunner.GroundModel = (function($, _, app){
var
    START_GROUND_X = 32,
    TILE_SIZE = 48,

    difficulty = 1,
    groundData = [true],
    airData = [false],

    observable = {},

    init = function(){
        for(var i = 0, max = START_GROUND_X; i < max; i++){
            generate();
        }
    },

    increaseDifficulty = function(){
        difficulty++;
    },

    decreaseDifficulty = function(){
        difficulty--;
    },

    getGroundData = function(){
        return groundData;
    },

    getAirData = function(){
        return airData;
    },

    isGroundSolid = function(tileWidthIndex){
        return groundData[tileWidthIndex];
    },

    isAirSolid = function(tileWidthIndex){
        return airData[tileWidthIndex];
    },

    generate = function(){
        var propVal = difficulty/100;
        var propThresh = propVal / (propVal + 1);
        groundData.push(Math.random() > propThresh || groundData.length < 25);
        airData.push(Math.random() > propThresh);
        $(observable).trigger("groundGenerated", {
            "previous" : {
                "index"  : groundData.length - 2,
                "air"    : airData[airData.length - 2],
                "ground" : groundData[groundData.length - 2]
            },
            "index"  : groundData.length - 1,
            "air"    : airData[airData.length - 1],
            "ground" : groundData[groundData.length - 1]
        });
    }

    return {
        init : init,
        generate : generate,
        increaseDifficulty : increaseDifficulty,
        decreaseDifficulty : decreaseDifficulty,
        isGroundSolid : isGroundSolid,
        isAirSolid : isAirSolid,
        getGroundData : getGroundData,
        getAirData : getAirData,
        TILE_SIZE : function(){ return TILE_SIZE }(),
        on : function(event, callback){
            $(observable).on(event, callback);
        }
    }

}(jQuery, _, DodoRunner));

DodoRunner.GroundView = (function($, _, app, easel){
var
    model = undefined,
    tilesheet = undefined,

    init = function(_model){
        model = _model;
        initResources();
        initEvents();
    },

    render = function(event, data){
        createGround(data.previous.index, data.previous.ground, data.previous.air, data);
    },

    createGround = function(index, ground, air, next){
        var x = index * model.TILE_SIZE;
        var y = app.Canvas.HEIGHT - 2*model.TILE_SIZE;
        var tileTypeTop = undefined;
        var tileTypeBottom = undefined;
        if(tileTypeTop == undefined &&  ground &&  next.ground) {
            tileTypeTop = "top";
            tileTypeBottom = "bottom";
        }
        if(tileTypeTop == undefined &&  ground && !next.ground) {
            tileTypeTop = "sidesTopLeft";
            tileTypeBottom = "sidesBottomLeft";
        }
        if(tileTypeTop == undefined && !ground &&  next.ground) {
            tileTypeTop = "sidesTopRight";
            tileTypeBottom = "sidesBottomRight";
        }
        if(tileTypeTop != undefined) {
            var topTile = createRandomTile(tileTypeTop);
            topTile.x = x;
            topTile.y = y;
            app.Canvas.addChild(topTile);
        }
        else{
            tileTypeBottom = "gap";
        }
        var bottomTile = createRandomTile(tileTypeBottom);
        bottomTile.x = x;
        bottomTile.y = y + model.TILE_SIZE;
        app.Canvas.addChild(bottomTile);
    },

    initEvents = function(){
        model.on("groundGenerated", render);
    },

    createRandomTile = function(animationName){
        var animation = tilesheet.getAnimation(animationName);
        var tile = new easel.BitmapAnimation(tilesheet);
        var frame = Math.floor(Math.random() * (animation.frames.length - 1)) + animation.frames[0];
        tile.gotoAndStop(frame);
        return tile;
    },

    initResources = function(){
        tilesheet = new easel.SpriteSheet({
            images: [app.AssetManager.getImageByName("ground")],
            frames: {
                width: model.TILE_SIZE,
                height: model.TILE_SIZE
            },
            animations: {
                top             :[0, 5],
                bottom          :[5, 6],
                sidesTopLeft    :[6, 7],
                sidesTopRight   :[7, 8],
                sidesBottomLeft :[8, 9],
                sidesBottomRight:[9, 10],
                gap             :[10, 11]
            }
        });
    }

    return {
        init : init
    }

}(jQuery, _, DodoRunner, createjs));